package com.hc.music.controller;

import com.hc.music.common.R;
import com.hc.music.entity.User;
import com.hc.music.entity.UserReset;
import com.hc.music.service.UserService;
import com.hc.music.service.impl.SimpleOrderManager;
import com.hc.music.util.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SimpleOrderManager simpleOrderManager;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("insert")
    public R insertUser(@RequestBody User user){
        user.setAvator("img/userPic/user.jpg");
        return userService.addUser(user);
    }

    @RequestMapping("selectAll")
    public R selectAll(){
        return userService.allUser();
    }

    @PostMapping("updateUserImage")
    public R updateUserAvator(@RequestParam("file")MultipartFile file, @RequestParam("id") Integer id){
        return userService.updateUserAvator(file,id);
    }

    @RequestMapping("deleteById")
    public R deleteById(@RequestParam Integer id){
        return userService.deleteUser(id);
    }

    @RequestMapping("loginByPassword")
    public R loginByUsernameAndPassword(@RequestBody User user){
        return userService.verityPasswd(user.getUsername(),user.getPassword());
    }

    @RequestMapping("loginByEmail")
    public R loginByEmail(@RequestParam String passowrd, @RequestParam String email,HttpSession session){
        User user = new User();
        user.setEmail(email);
        user.setPassword(passowrd);
        return userService.loginEmailStatus(user,session);
    }

    @RequestMapping("sendVerificationCode")
    public R sendVerificationCode(@RequestParam String email){
        User user = userService.findByEmail(email);
        if(user == null){
            return R.error("用户不存在");
        }
        String code = RandomUtils.code();
        simpleOrderManager.sendCode(code,email);
        stringRedisTemplate.opsForValue().set("code",code,5, TimeUnit.MINUTES);
        return R.success("验证码发送成功");
    }

    @RequestMapping("resetPassword")
    public R resetPassword(@RequestBody UserReset userReset){
        User result = userService.findByEmail(userReset.getEmail());
        if(result == null){
            return R.error("用户不存在");
        }
        String code = stringRedisTemplate.opsForValue().get("code");
        if(!userReset.getCode().equals(code)){
            return R.error("验证码错误或失效");
        }
        User user =new User();
        BeanUtils.copyProperties(user, result);
        user.setPassword(userReset.getPassword());
        return userService.updatePassword(user);
    }

    @RequestMapping("selectById")
    public R selectById(@RequestParam Integer id){
        return userService.userOfId(id);
    }

    @RequestMapping("updateInfo")
    public R updateUserInfo(@RequestBody User user){
        return userService.updateUserMsg(user);
    }

    @RequestMapping("updatePassword")
    public R updateUserPwd(@RequestBody User user){
        return userService.updatePassword01(user);
    }
}
